﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SqlDataAccess;

namespace QuanLyBanHang
{
    public partial class F500_Danh_muc_loai_san_pham : Form
    {
        public F500_Danh_muc_loai_san_pham()
        {
            InitializeComponent();
            set_define_events();
        }

        #region Public Interfaces

        #endregion

        #region Members
        BKIQuanLyBanHangEntities db = new BKIQuanLyBanHangEntities();
        #endregion

        #region Private Methods

        private void set_define_events()
        {
            this.Load += F500_Danh_muc_loai_san_pham_Load;
            this.m_cmd_cap_nhat_loai_san_pham.Click += m_cmd_cap_nhat_loai_san_pham_Click;
            this.m_cmd_them_loai_san_pham.Click += m_cmd_them_loai_san_pham_Click;
            this.m_cmd_xoa_loai_san_pham.Click += m_cmd_xoa_loai_san_pham_Click;
            this.m_cmd_xoa_trang.Click += m_cmd_xoa_trang_Click;
            this.m_cmd_quay_lai.Click += m_cmd_quay_lai_Click;
            this.m_cmd_tim_kiem_loai_san_pham.Click += m_cmd_tim_kiem_loai_san_pham_Click;
            this.m_txt_tim_kiem_loai_san_pham.TextChanged += m_txt_tim_kiem_loai_san_pham_TextChanged;
            this.m_grv_loai_san_pham.Click += m_grv_loai_san_pham_Click;

            //this.Load += new System.EventHandler(this.F500_Danh_muc_loai_san_pham_Load);
            //this.m_cmd_quay_lai.Click += new System.EventHandler(this.m_cmd_quay_lai_Click);
            //this.m_cmd_tim_kiem_loai_san_pham.Click += new System.EventHandler(this.m_cmd_tim_kiem_loai_san_pham_Click);
            //this.m_cmd_xoa_loai_san_pham.Click += new System.EventHandler(this.m_cmd_xoa_loai_san_pham_Click);
            //this.m_cmd_them_loai_san_pham.Click += new System.EventHandler(this.m_cmd_them_loai_san_pham_Click);
            //this.m_cmd_xoa_trang.Click += new System.EventHandler(this.m_cmd_xoa_trang_Click);
            //this.m_cmd_cap_nhat_loai_san_pham.Click += new System.EventHandler(this.m_cmd_cap_nhat_loai_san_pham_Click);
            //this.m_grv_loai_san_pham.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.m_grv_loai_san_pham_CellContentClick);
        }

        private bool check_validate_data_is_ok()
        {
            if (string.IsNullOrWhiteSpace(m_txt_ma_loai_san_pham.Text))
            {
                MessageBox.Show("Bạn phải nhập mã loại sản phẩm!", "Thông báo");
                m_txt_ma_loai_san_pham.Focus();
                return false;
            }
            if (string.IsNullOrWhiteSpace(m_txt_ten_loai_san_pham.Text))
            {
                MessageBox.Show("Bạn phải nhập tên loại sản phẩm!", "Thông báo");
                m_txt_ten_loai_san_pham.Focus();
                return false;
            }
            return true;
        }

        private void load_data_to_grid()
        {
            var v_lst_danh_muc_loai_san_pham = db.DM_LOAI_SAN_PHAM
                .Where(x => x.MA_LOAI_SAN_PHAM.Contains(m_txt_tim_kiem_loai_san_pham.Text.Trim())
                || x.TEN_LOAI_SAN_PHAM.Contains(m_txt_tim_kiem_loai_san_pham.Text.Trim())
                || x.DAC_DIEM.Contains(m_txt_tim_kiem_loai_san_pham.Text.Trim())
                || m_txt_tim_kiem_loai_san_pham.Text == "")
                .OrderBy(x => x.MA_LOAI_SAN_PHAM)
                .Select(x => new
                {
                    ID = x.ID,
                    MA_LOAI_SAN_PHAM = x.MA_LOAI_SAN_PHAM,
                    TEN_LOAI_SAN_PHAM = x.TEN_LOAI_SAN_PHAM,
                    DAC_DIEM = x.DAC_DIEM
                })
                .ToList();
            m_grv_loai_san_pham.DataSource = v_lst_danh_muc_loai_san_pham;
            m_grv_loai_san_pham.Columns[0].HeaderText = "ID";
            m_grv_loai_san_pham.Columns[0].Visible = false;
            m_grv_loai_san_pham.Columns[0].Width = 20;
            m_grv_loai_san_pham.Columns[1].HeaderText = "Mã loại sản phẩm";
            m_grv_loai_san_pham.Columns[1].Width = 100;
            m_grv_loai_san_pham.Columns[2].HeaderText = "Tên loại sản phẩm";
            m_grv_loai_san_pham.Columns[2].Width = 100;
            m_grv_loai_san_pham.Columns[3].HeaderText = "Đặc điểm";
            m_grv_loai_san_pham.Columns[3].Width = 300;
        }

        #endregion

        #region Events

        private void F500_Danh_muc_loai_san_pham_Load(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        void m_grv_loai_san_pham_Click(object sender, EventArgs e)
        {
            try
            {
                decimal id_loai_sp = (decimal)m_grv_loai_san_pham.CurrentRow.Cells[0].Value; //Xac dinh id loai san pham
                DM_LOAI_SAN_PHAM v_dm_loai_sp = db.DM_LOAI_SAN_PHAM.FirstOrDefault(x => x.ID == id_loai_sp);
                if (v_dm_loai_sp != null)
                {
                    m_txt_ma_loai_san_pham.Text = v_dm_loai_sp.MA_LOAI_SAN_PHAM;
                    m_txt_ten_loai_san_pham.Text = v_dm_loai_sp.TEN_LOAI_SAN_PHAM;
                    m_txt_dac_diem.Text = v_dm_loai_sp.DAC_DIEM;
                }
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_quay_lai_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_xoa_trang_Click(object sender, EventArgs e)
        {
            try
            {
                m_txt_ma_loai_san_pham.Text = "";
                m_txt_ten_loai_san_pham.Text = "";
                m_txt_dac_diem.Text = "";
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_xoa_loai_san_pham_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_grv_loai_san_pham.CurrentRow == null)
                {
                    MessageBox.Show("Bạn hãy chọn loại sản phẩm để xóa!", "Thông báo!");
                    return;
                }
                decimal id_loai_san_pham = (decimal)m_grv_loai_san_pham.CurrentRow.Cells[0].Value;
                var v_dm_loai_san_pham = db.DM_LOAI_SAN_PHAM.FirstOrDefault(x => x.ID == id_loai_san_pham);
                if (v_dm_loai_san_pham != null)
                {
                    if (MessageBox.Show("Bạn có chắc chắn muốn XÓA LOẠI SẢN PHẨM này và các SẢN PHẨM liên quan không?", "Cảnh báo!", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        //Xóa dữ liệu bảng DM_SAN_PHAM
                        var v_lst_san_pham = db.DM_SAN_PHAM
                            .Where(x => x.ID_LOAI_SAN_PHAM == id_loai_san_pham)
                            .ToList();
                        for (int i = 0; i < v_lst_san_pham.Count; i++)
                        {
                            db.DM_SAN_PHAM.Remove(v_lst_san_pham[i]);
                            db.SaveChanges();
                        }
                        //Xóa dữ liệu bảng DM_LOAI_SAN_PHAM
                        var v_lst_dm_loai_san_pham = db.DM_LOAI_SAN_PHAM
                            .Where(x => x.ID == id_loai_san_pham)
                            .ToList();
                        for (int i = 0; i < v_lst_dm_loai_san_pham.Count; i++)
                        {
                            db.DM_LOAI_SAN_PHAM.Remove(v_lst_dm_loai_san_pham[i]);
                            db.SaveChanges();
                            MessageBox.Show("Bạn đã xóa dữ liệu thành công!", "Thông báo!");
                            load_data_to_grid();
                        }
                    }
                }
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_tim_kiem_loai_san_pham_Click(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_cap_nhat_loai_san_pham_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_grv_loai_san_pham.CurrentRow == null)
                    return;
                decimal id_loai_san_pham = (decimal)m_grv_loai_san_pham.CurrentRow.Cells[0].Value;
                var v_dm_loai_san_pham = db.DM_LOAI_SAN_PHAM.FirstOrDefault(x => x.ID == id_loai_san_pham);
                if (v_dm_loai_san_pham != null)
                {
                    if (check_validate_data_is_ok())
                    {
                        v_dm_loai_san_pham.MA_LOAI_SAN_PHAM = m_txt_ma_loai_san_pham.Text.Trim();
                        v_dm_loai_san_pham.TEN_LOAI_SAN_PHAM = m_txt_ten_loai_san_pham.Text.Trim();
                        v_dm_loai_san_pham.DAC_DIEM = m_txt_dac_diem.Text.Trim();
                        db.SaveChanges();
                        m_cmd_xoa_trang_Click(null, null);
                        MessageBox.Show("Bạn đã cập nhật thành công loại sản phẩm!", "Thông báo!");
                        load_data_to_grid();
                    }
                }
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_them_loai_san_pham_Click(object sender, EventArgs e)
        {
            try
            {
                if (check_validate_data_is_ok())
                {
                    DM_LOAI_SAN_PHAM v_dm_loai_san_pham = new DM_LOAI_SAN_PHAM();
                    v_dm_loai_san_pham.MA_LOAI_SAN_PHAM = m_txt_ma_loai_san_pham.Text.Trim();
                    v_dm_loai_san_pham.TEN_LOAI_SAN_PHAM = m_txt_ten_loai_san_pham.Text.Trim();
                    v_dm_loai_san_pham.DAC_DIEM = m_txt_dac_diem.Text.Trim();
                    db.DM_LOAI_SAN_PHAM.Add(v_dm_loai_san_pham);
                    m_cmd_xoa_trang_Click(null, null);
                    db.SaveChanges();
                    MessageBox.Show("Bạn đã thêm thành công loại sản phẩm!","Thông báo!");
                    load_data_to_grid();
                }
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_grv_loai_san_pham_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (m_grv_loai_san_pham.CurrentRow == null)
                    return;
                decimal id_loai_san_pham = (decimal)m_grv_loai_san_pham.CurrentRow.Cells[0].Value;
                var v_dm_loai_san_pham = db.DM_LOAI_SAN_PHAM.FirstOrDefault(x => x.ID == id_loai_san_pham);
                if (v_dm_loai_san_pham != null)
                {
                    m_txt_ma_loai_san_pham.Text = v_dm_loai_san_pham.MA_LOAI_SAN_PHAM;
                    m_txt_ten_loai_san_pham.Text = v_dm_loai_san_pham.TEN_LOAI_SAN_PHAM;
                    m_txt_dac_diem.Text = v_dm_loai_san_pham.DAC_DIEM;
                }
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_txt_tim_kiem_loai_san_pham_TextChanged(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        #endregion

        
    }
}